
import cv2
import os

def img_rotate(src, dst, direction):
    mat = cv2.imread(src)
    dmat = cv2.rotate(mat, direction)
    cv2.imwrite(dst, dmat)


directory="/Users/v/Documents/proj/testdata/origin_imgs/images/"

for dirpath, dirnames, filenames in os.walk(directory):
    # dirpath 是一个字符串，表示当前遍历到的文件夹路径
    # dirnames 是一个列表，包含 `dirpath` 下的子目录名
    # filenames 是一个列表，包含 `dirpath` 下的非目录文件名
    print(f"遍历到的目录路径: {dirpath}")

    # 打印当前目录下的所有文件
    for filename in filenames:
        print(f"找到文件: {os.path.join(dirpath, filename)}")
        file_name_pre, file_extension = os.path.splitext(filename)
        print(f"file name {file_name_pre} , {file_extension}")
        if file_extension == '.jpeg' or file_extension == '.jpg' or file_extension == '.png' \
            or file_extension == '.JPEG' or file_extension == '.JPG' or file_extension == '.PNG':
            src_file=directory + "/" + filename
            # rotate 90
            dst_file = directory + "/" + file_name_pre + "-r90" + file_extension
            img_rotate(src_file, dst_file, cv2.ROTATE_90_CLOCKWISE)
            dst_file = directory + "/" + file_name_pre + "-r180" + file_extension
            img_rotate(src_file, dst_file, cv2.ROTATE_180)
            dst_file = directory + "/" + file_name_pre + "-r270" + file_extension
            img_rotate(src_file, dst_file, cv2.ROTATE_90_COUNTERCLOCKWISE)
